//
//  MLAStoreHomeVC.m
//  MagicLampApp
//
//  Created by Zheng on 2019/1/15.
//  Copyright © 2019 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAStoreHomeVC.h"
#import "MLAStoreHomeModel.h"

@interface MLAStoreHomeVC ()
@property (nonatomic, strong) MLAStoreHomeModel *model;


@end

@implementation MLAStoreHomeVC {
    UILabel *shopStateLbl;
    UILabel *shopNameLbl;
    UISwitch *shopSwitch;
    UIImageView *shopLogoImgv;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configTitle:@"店铺信息"];
    
    self.titlesArr = @[@"店铺类型 ：",@"经营地址 ：",@"详细地址 ："];

    [self configShopInfoView];
    [self.tableView reloadData];
    
    [self loadData];
}

- (void)loadData {
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kShopDetails params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.model = [MLAStoreHomeModel mj_objectWithKeyValues:response];
        NSString *stateName = self.model.business.integerValue == 1? @"营业":@"打烊";
        self->shopStateLbl.text = [NSString stringWithFormat:@"店铺状态：%@",stateName];
        self->shopSwitch.on = self.model.business.integerValue == 1? YES: NO;
        self->shopNameLbl.text = self.model.shop_name;
        [self->shopLogoImgv sd_setImageWithURL:[NSURL URLWithString:self.model.shop_logo]];
        
        NSMutableArray *dataArr = [NSMutableArray array];
        if (self.model.shop_type_name) {
            NSString *str = [NSString stringWithFormat:@"店铺类型 ：%@",self.model.shop_type_name];
            [dataArr addObject:str];
        }
        
        if (self.model.province&&self.model.city&&self.model.area) {
            NSString *str = [NSString stringWithFormat:@"经营地址 ：%@%@%@",self.model.province,self.model.city,self.model.area];
            [dataArr addObject:str];
        }
        
        if (self.model.address) {
            NSString *str = [NSString stringWithFormat:@"详细地址 ：%@",self.model.shop_type_name];
            [dataArr addObject:str];
            
        }
        
        self.titlesArr = [dataArr copy];
        
        [self.tableView reloadData];
    } failure:^{
        
    }];
}

#pragma mark - 设置店铺信息视图
- (void)configShopInfoView {
    
    UIView *tableHeaderView = [[UIView alloc] init];
    tableHeaderView.frame = CGRectMake(0, 0, CX_GetScreenWidth(), 120);
    
    shopLogoImgv = [[UIImageView alloc] init];
    shopLogoImgv.backgroundColor = CX_GetColorLine();
    [tableHeaderView addSubview:shopLogoImgv];
    [shopLogoImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(75);
        make.left.mas_equalTo(15);
        make.centerY.equalTo(tableHeaderView);
    }];
    
    shopNameLbl = [[UILabel alloc] init];
    shopNameLbl.font = CX_GetNormalFont(18);
    shopNameLbl.textColor = CX_GetColorTitle();
    [tableHeaderView addSubview:shopNameLbl];
    [shopNameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->shopLogoImgv.mas_right).offset(20);
        make.top.equalTo(self->shopLogoImgv);
        make.right.mas_equalTo(-15);
    }];
    
    shopStateLbl = [[UILabel alloc] init];
    shopStateLbl.font = CX_GetNormalFont(12);
    shopStateLbl.textColor = CX_GetColorHex(0x999999, 1);

    [tableHeaderView addSubview:shopStateLbl];
    [shopStateLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->shopLogoImgv.mas_right).offset(20);
        make.top.equalTo(self->shopNameLbl.mas_bottom).offset(20);
        make.width.mas_equalTo(100);
    }];
    
    shopSwitch = [[UISwitch alloc] init];
    shopSwitch.tintColor = CX_GetColorBlue();
    shopSwitch.onTintColor = CX_GetColorBlue();
    [shopSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
    [tableHeaderView addSubview:shopSwitch];
    [shopSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.bottom.equalTo(self->shopLogoImgv).offset(-4);
    }];
    
    
    
    
    self.tableView.tableHeaderView = tableHeaderView;
    [tableHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.width.mas_equalTo(CX_GetScreenWidth()).priorityHigh();
        make.height.mas_equalTo(120);
    }];
    
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryView = nil;
}


- (void)switchAction:(UISwitch *)sender {
    
    NSString *isOn;
    if (sender.isOn) {
        CX_Log(@"开启");
        isOn = @"1";
    } else {
        CX_Log(@"关闭");
        isOn = @"2";
    }
    
    NSDictionary *params = @{@"business":isOn};
    
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kShopSave params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        [self loadData];
    } failure:^{
        
    }];
    
}

@end
